<template>
  <el-radio-group v-model="queryParams.sourceTypes" class="mb-15px" @change="handleQuery">
    <el-radio-button label="工作库" value="1" />
    <el-radio-button label="资源库" value="2" />
    <el-radio-button label="回收库" value="3" />
  </el-radio-group>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="资源名称" prop="sourceName">
        <el-input
          v-model="queryParams.sourceName"
          placeholder="请输入资源名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="资源编号" prop="sourceCode">
        <el-input
          v-model="queryParams.sourceCode"
          placeholder="请输入资源编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="资源类型" prop="sourceType">
        <el-select
          v-model="queryParams.sourceType"
          placeholder="请选择资源类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.SOURCE_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="主题分类" prop="sourceType">
        <el-select
          v-model="queryParams.sourceType"
          placeholder="请选择主题分类"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.JOURNAL_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="资源标签" prop="sourceTag">
        <el-tree-select
          v-model="queryParams.sourceTag"
          :data="tagList"
          :props="{
            label: 'tagName',
            value: 'id',
            children: 'children',
          }"
          :render-after-expand="false"
          clearable
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="资源来源" prop="sourceFrom">
        <!-- <el-select
          v-model="queryParams.sourceFrom"
          placeholder="请选择资源来源"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.SOURCE_FROM)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select> -->
        <el-input
          v-model="queryParams.sourceFrom"
          placeholder="请输入资源来源"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.BPM_PROCESS_INSTANCE_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['dr:source-cata:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['dr:source-cata:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <el-table-column label="封面" align="center" prop="sourceData.mainImage">
        <template #default="scope">
          <el-image
            style="width: 44px; height: 44px"
            :src="scope.row.sourceData?.mainImage"
            :preview-src-list="[scope.row.sourceData?.mainImage]"
            fit="cover"
          />
        </template>
      </el-table-column>
      <el-table-column label="资源编号" align="center" prop="sourceData.sourceCode" />
      <el-table-column label="资源名称" align="center" prop="sourceData.sourceName"/>
      <el-table-column label="资源类型" align="center" prop="sourceData.sourceType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SOURCE_TYPE" :value="scope.row.sourceData?.sourceType" />
        </template>
      </el-table-column>
      <el-table-column label="来源信息" align="center" prop="sourceData.sourceDesc">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SOURCE_DESC" :value="scope.row.sourceData?.sourceDesc" />
        </template>
      </el-table-column>
      <el-table-column label="资源来源" align="center" prop="sourceData.sourceFrom">
        <template #default="scope">
          <!-- <dict-tag :type="DICT_TYPE.SOURCE_FROM" :value="scope.row.sourceData?.sourceFrom" /> -->
           {{ scope.row.sourceData?.sourceFrom }}
        </template>
      </el-table-column>
      <el-table-column label="文件类型" align="center" prop="sourceData.fileType" />
      <el-table-column label="主题分类" align="center" prop="catalogClassify">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.JOURNAL_TYPE" :value="scope.row.catalogClassify" />
        </template>
      </el-table-column>
      <el-table-column label="资源标签" align="center" prop="catalogTags">
        <template #default="scope">
          <DictCataTag :value="scope.row.catalogTags" />
        </template>
      </el-table-column>
      <el-table-column label="存放位置" align="center" prop="catalogStore" />
      <el-table-column label="资源描述" align="center" prop="catalogDesc">
        <template #default="scope">
          <span v-html="scope.row.catalogDesc"></span>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.BPM_PROCESS_INSTANCE_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      
      <el-table-column label="操作" align="center" min-width="240px" fixed="right">
        <template #default="scope">
          <el-button
            v-if="['1', '2'].includes(queryParams.sourceTypes)"
            link
            type="primary"
            @click="openDetail(scope.row)"
            v-hasPermi="['dr:source-cata:query']"
          >
            详情
          </el-button>
          <el-button
            link
            type="primary"
            v-if="['1', '2'].includes(queryParams.sourceTypes) && [-1, 4].includes(scope.row.status)"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['dr:source-cata:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="primary"
            v-if="['3'].includes(queryParams.sourceTypes)"
            @click="handleBack(scope.row)"
            v-hasPermi="['dr:source-cata:update']"
          >
            还原
          </el-button>
          <el-button
            link
            type="danger"
            v-if="['1', '2'].includes(queryParams.sourceTypes) && [-1, 3, 4].includes(scope.row.status)"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['dr:source-cata:delete']"
          >
            删除
          </el-button>
          <el-button
            link
            type="danger"
            v-if="['1', '2'].includes(queryParams.sourceTypes) && [3].includes(scope.row.status)"
            @click="openForm('create', scope.row.id)"
            v-hasPermi="['dr:source-cata:create']"
          >
            重新发起
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <SourceCataForm ref="formRef" @success="getList" />
  <!-- 详情 -->
  <SourceCataDetail v-model:visible="detailVisible" :data="currentRow" />
 
</template>

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { handleTree } from '@/utils/tree'
import { SourceApi, SourceVO } from '@/api/dr/source'
import { SourceCataApi, SourceCataVO } from '@/api/dr/sourcecata'
import { SourceTagApi, SourceTagVO } from '@/api/dr/sourcetag'
import SourceCataForm from './SourceCataForm.vue'

/** 资源信息 列表 */
defineOptions({ name: 'SourceCata' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<SourceCataVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  sourceTypes: '1',
  sourceId: undefined,
  sourceName: undefined,
  sourceCode: undefined,
  sourceType: undefined,
  sourceTag: undefined,
  sourceDesc: undefined,
  fileType: undefined,
  fileSize: undefined,
  file: undefined,
  sourceFrom: undefined,
  status: undefined,
  createTime: [],
})
const tagList = ref<SourceTagVO[]>([])
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref<boolean>(false) // 导出的加载中
const detailVisible = ref<boolean>(false)
const currentRow = ref<SourceCataVO>()

/** 查询tag列表 */
const getTagList = async () => {
  try {
    const data = await SourceTagApi.getSourceTagPage({
      pageNo: 1,
      pageSize: 100,
    })
    tagList.value = handleTree(data.list, 'id', 'parentId', 'children')
  } finally {
  }
}
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await SourceCataApi.getPage(queryParams)
    const dataList = (data.list || []).map(async(v) => ({
      ...v,
      sourceData: v.sourceId ? await SourceApi.getSource(v.sourceId) : {},
    }))
    list.value = await Promise.all(dataList)
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}
// 详情弹窗
const openDetail = (row: SourceCataVO) => {
  currentRow.value = { ...row }
  detailVisible.value = true
}
// 还原
const handleBack= async (row: SourceCataVO) => {
  try {
    // 删除的二次确认
    await message.confirm('确认执行还原操作?')
    await SourceCataApi.reback(row)
    message.success(t('common.success'))
    // 刷新列表
    await getList()
  } catch {}
}
/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await SourceCataApi.delete(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await SourceCataApi.export(queryParams)
    download.excel(data, '资源编目.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getTagList()
  getList()
})
</script>
